package traverser;

public class WorkerThread extends Thread {
	
	public boolean fresh   = false;
	public boolean idle    = true;
	public boolean running = true;
	private String link;
	private String parent;
	
	public static MasterThread master;
	
	public void seedLink (String link, String parent) {
		fresh = true;
		idle = false;
		this.link = link;
		this.parent = parent;
	}
	
	
	public void run () {
		String page;
		while (running) {
			while (!fresh) {
				if (!running) {
					return;
				}
				Thread.yield();
			}
			
			synchronized (this) {
				fresh = false;
				page = link;
			}
			
			// Visit page and compute priority
			int priority = master.computePriority(page);
			
			
			synchronized (master.toVisit) {
				// Add page with priority to toVisit
				master.toVisit.add(page, parent, priority);
			}
			
			synchronized (this) {
				idle = true;
			}
			master.idle_workers.release();
				
			
			
		}
	}
	

}
